/**
 * 用于页面级别的JS
 * wolfer
 * wolfers@126.com
 * 2013-04-10
 */

define(["js/mCard.js","js/mCommon.js","js/mVideo.js"],function(card,common,video){  
	
	var isOpenGame;
	var game = {
			list:function(){
				var _t = this;
				var url = Game.getBaseURL() + "/room/competitionList";
				$.ajax({
					url: url,
					dataType:"json",
					success: function(data){
						if(data){
							 _t.createGameList(data);
						}
					}
				});
			},
			
			/*创建比赛列表*/
			createGameList:function(data){
				var html = [];
				for(var i=data.length-1; i>=0; i--){
					if(data[i].name === "高级比赛场"){
						var textClass = "text-gjbsc";
					}else if(data[i].name === "中级比赛场"){
						var textClass = "text-zjbsc";
					}else if(data[i].name === "初级比赛场"){
						var textClass = "text-cjbsc";
					}
					
					if(data[i].isCanSignUp){
						var button = "<span class='text-bm num_icon'></span>";
					}else{
						var button = "<span class='text-bjb num_icon'></span>";
					}
					
					html.push("<li>"+
						"<span class='"+textClass+" num_icon'></span>"+
						"<p>进入限制"+data[i].beanFunLimitDesc+"</p>"+
						"<span roomId='"+data[i].roomId+"' allNum = '"+data[i].signUpNumber+"'  class='gameButton pokeyButton pokeyButton-1 button-blue-1 icon'>"+button+"</span>"+
					"</li>");
				}
				$("#gameLists").html(html.join(""));
				
				/*事件绑定*/
				var _t = this;
				$(".gameButton").click(function(){
					_t._indexButton  = $(".gameButton").index($(this));
					var roomId = $(this).attr("roomId");
					var allNum = $(this).attr("allNum");
					console.log(roomId);
					_t.signUp(roomId);
					_t._allNum = allNum;
					_t._roomId = roomId;
				})
			},
			
			/*报名*/
			signUp:function(roomId){
				var _t = this;
				var forTime=(new Date()).getTime();
				var url = Game.getBaseURL() + "/room/signUp?roomId="+roomId+"&&time="+forTime;;
				$.ajax({
					url: url,
					dataType:"json",
					success: function(data){
						
						if(data.success){
							_t.gameRoomId = roomId;
							_t.gameStat(2);
							/*现在的人数*/
							_t.gameAllNums();
							//_t.gameNowNums(5);
						}else{
							$().alert({
								content : data.message
							});
						}
					}
				});
				
			},
			
			gameStat:function(n){
				if(n === 1){
					//$("#room").show();
					//$("#playBox").hide();
					$(".gameRoom").show();
					$(".gameWait").hide();
					$(".gameAccounts").hide();
				}else if(n === 2){
					//$("#room").show();
					//$("#playBox").hide();
					$(".gameRoom").hide();
					$(".gameWait").show();
					$(".gameAccounts").hide();
				}else if(n === 3){
					$("#room").show();
					$("#playBox").hide();
					$(".gameRoom").hide();
					$(".gameWait").hide();
					$(".gameAccounts").show();
				}
			},
			
			gameNowNums:function(obj){
				var num = obj.competitionSignUpNumber;
				$("#nowNums").html(num);
				var allLength = 590;
				var now = allLength/this._allNum*num;
				$("#loadBaring").width(now);
				isOpenGame = false;
				/*当人数已经满的时候  就开局*/
				if(num == this._allNum){
					isOpenGame = true;
					initGame();
				}
			},
			
			gameWiatNums:function(obj){
				
				var num = obj.competitionCompletedPersonNum;
				var surplus = obj.competitionUncompletedPersonNum;
				$("#allNumsWait").html(num+surplus);
				$("#nowNumsWait").html(num);
				var allLength = 590;
				var now = allLength/(num+surplus)*num;
				$("#loadBaringWait").width(now);
				isOpenGame = false;
				/*当人数已经满的时候  就开局*/
				if(surplus == 0){
					isOpenGame = true;
					initGame();
				}
				
			},
			
			gameAllNums:function(){
				$("#allNums").html(this._allNum);
			},
			
			
			
			event:function(){
				var gameRank = $("#gameRank");
				var gameOut = $("#gameOut,#gameOut_2");
				var gameAwards = $("#gameAwards");
				var _t = this;
				/*room/competitionRanks
				GET
				要携带roomId
				room/quit
				POST
				要携带roomId
				*/
				gameRank.live("click",function(){
					
					var roomId = game.gameRoomId;
					var _t = this;
					var forTime=(new Date()).getTime();
					var url = Game.getBaseURL() + "/room/competitionRanks?roomId="+roomId+"&&time="+forTime;
					$.ajax({
						url: url,
						dataType:"json",
						success: function(data){
							if(data.success){
								game._createRank(data.data.ranks);
							}else{
								$().alert({"content":data.message});
							}
						}
					});
				});
				
				gameOut.live("click",function(){
					var roomId = game.gameRoomId;
					var _t = this;
					var forTime=(new Date()).getTime();
					var url = Game.getBaseURL() + "/room/quit";
					$.ajax({
						url: url,
						dataType:"json",
						type: "POST",
						data: "roomId="+roomId,
						success: function(data){
							if(data.success){
								window.location.href = Game.getBaseURL()+"/game.jsp?"+forTime;
							}else{
								$().alert({"content":data.message});
							}
						}
					});
				});
				
				/*奖励情况*/
				gameAwards.live("click",function(){
					require(["js/mBox.js"], function(box){
						box.lookAwards(_t._indexButton);
					});
				});
				
				
				
			},
			
			goOnGame:function(){
				$("#room").hide();
				$("#playBox").show();
				$("body").attr("class","playBg");
				
				room._setBombNums();
				player.setInfo().multiple("my",1);
				player.setInfo().multiple("left",1);
				player.setInfo().multiple("right",1);
				
			},
			
			_createRank:function(data){
				var html = "<table class='table-box-rank'  width='100%' border='0' cellspacing='0' cellpadding='0'>"+
							  "<thead><tr>"+
							    "<th style='width:20%'>名次</th>"+
							    "<th style='width:40%'>昵称</th>"+
							    "<th style='width:40%'>比赛积分</th>"+
							  "</tr>"+
							  "</thead>"+
							 "</table>";
				html+="<table class='table-box-rank' width='100%' border='0' cellspacing='0' cellpadding='0'>"
				for(var i=0;i<data.length; i++){
					if(i%2 === 0){
						var bg = "style='background:#e6c3bd'";
					}else{
						var bg = "";
					}
					html+="<tr "+bg+">"+
							    "<td style='width:20%'>"+(i+1)+"</td>"+
							    "<td style='width:40%'>"+data[i].nickname+"</td>"+
							    "<td style='width:40%'>"+data[i].competitionScore+"</td>"+
							  "</tr>";
				}
				html+="</table>";
				
				require(["js/mBox.js"], function(box){
					box.currentRanking(html);
				});
				
			}
	}
	
	
	
	
	
	
	
	
	
	return {
		
		
		game:function(){
			game.list();
			game.event();
			connectToServer();
			var boss_return = $("#boss_return");
			boss_return.live(common.getEvent[0],function(){
	    		$(this).addClass("on");
	    	});
			boss_return.live(common.getEvent[1],function(){
	    		$(this).removeClass("on");
	    		window.history.go(-1);
	    	});
		},
		
		gameStat:function(n){
			game.gameStat(n);
		},
		
		gameNowNums:function(obj){
			game.gameNowNums(obj);
		},
		
		gameWiatNums:function(obj){
			game.gameWiatNums(obj);
		},
		
		gameStart:function(){
			enterRoom(game._roomId);
		},
		
		sign:function(){
			var roomId = game.gameRoomId;
			game.signUp(roomId);
		},
		
		isOpen:function(){
			return isOpenGame;
		},
		
		goOnGame:function(){
			game.goOnGame();
		}
		
	}
}); 